package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etoak.common.core.exception.CustomException;
import com.etoak.common.core.vo.PageVO;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Ingredients;
import com.etoak.mapper.IngredientsMapper;
import com.etoak.service.IngredientsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author etoak
 * @since 2025-06-30
 */
@Service
public class IngredientsServiceImpl extends ServiceImpl<IngredientsMapper, Ingredients> implements IngredientsService {

    @Override
    public ResultVO add(Ingredients ingredients) {
        if (ObjUtil.isNotEmpty(getByName(ingredients.getName()))) {
            throw new CustomException("食材名称重复");
        }
        ingredients.setCreateTime(DateUtil.now());
        if (this.save(ingredients)) {
            return ResultVO.success("添加成功");
        } else {
            return ResultVO.error("添加失败");
        }
    }

    @Override
    public Ingredients getByName(String name) {
        return lambdaQuery().eq(Ingredients::getName, name).one();
    }

    @Override
    public ResultVO<List<Ingredients>> listIngredients(Ingredients ingredients) {
        List<Ingredients> list = lambdaQuery()
                .likeRight(StrUtil.isNotEmpty(ingredients.getName()), Ingredients::getName, ingredients.getName())
                .eq(StrUtil.isNotEmpty(ingredients.getType()), Ingredients::getType, ingredients.getType())
                .list();
        return ResultVO.success(list);
    }

    @Override
    public ResultVO<PageVO<Ingredients>> listPage(int pageNum, int pageSize, Ingredients ingredients) {
        Page<Ingredients> page = new Page<>(pageNum, pageSize);
        page = lambdaQuery()
                .likeRight(StrUtil.isNotEmpty(ingredients.getName()), Ingredients::getName, ingredients.getName())
                .eq(StrUtil.isNotEmpty(ingredients.getType()), Ingredients::getType, ingredients.getType())
                .page(page);
        return ResultVO.success(PageVO.of(page.getRecords(), page.getTotal()));
    }

    @Override
    public ResultVO update(int id, Ingredients ingredients) {
        if (ObjUtil.isEmpty(getById(id))) {
            return ResultVO.error("食材不存在");
        }
        String name = ingredients.getName();
        if (StrUtil.isNotEmpty(name)) {
            Ingredients saveIngredients = getByName(name);
            if (ObjUtil.isNotEmpty(saveIngredients) && !saveIngredients.getId().equals(id)) {
                throw new CustomException("食材名称重复");
            }
        }
        ingredients.setId(id);
        ingredients.setUpdateTime(DateUtil.now());
        if(updateById(ingredients)){
            return ResultVO.success("更新成功");
        }else {
            return ResultVO.error("更新失败");
        }
    }

    @Override
    public ResultVO delete(int id) {
        if (ObjUtil.isEmpty(getById(id))) {
            return ResultVO.error("食材不存在");
        }
        if (removeById(id)) {
            return ResultVO.success("删除成功");
        } else {
            return ResultVO.error("删除失败");
        }
    }

    @Override
    public ResultVO<List<Ingredients>> listByType(String type) {
        List<Ingredients> list = lambdaQuery()
                .eq(StrUtil.isNotEmpty(type), Ingredients::getType, type)
                .list();
        return ResultVO.success(list);
    }
}
